<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: inc.get_categories.php 529 2015-02-28 11:35:18Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 529 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2015-02-28 11:35:18 +0000 (Sat, 28 Feb 2015) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
function get_categories($parent_id = '0', $level = 1, $img_subpath = '', $flat=false, $checkStatus = true) {
	global $db;

	$parent_id = $parent_id;
	$categories_array = array();

	if(GROUP_CHECK == 'true')
		$group_check = " AND c.group_permission_".$_SESSION['customers_status']['customers_status_id']." = 1 ";

	$categories = $db->db_query(
    "SELECT
      c.categories_id,
      cd.categories_name,
      c.categories_image,
      seo.url_text 
    FROM
      ".TABLE_CATEGORIES." c
      INNER JOIN ".TABLE_CATEGORIES_DESCRIPTION." AS cd
        ON (cd.categories_id = c.categories_id AND cd.language_id = ".(int)$_SESSION['languages_id'].")
      INNER JOIN ".TABLE_SEO_URL." AS seo
        ON (seo.categories_id = c.categories_id AND seo.language_id = ".(int)$_SESSION['languages_id'].")
    WHERE
      c.parent_id = ".$db->db_prepare($parent_id)."
      ".($checkStatus?"AND c.categories_status != 0":"")."
      ".$group_check."
    ORDER BY
      sort_order, cd.categories_name", true);

	while(!$categories->EOF) {
		$categories_array[$categories->fields['categories_id']] = array(
      'level'     => $level,
      'id'        => $categories->fields['categories_id'], 
      'text'      => $indent.$categories->fields['categories_name'],
      'cat_id'    => $categories->fields['categories_id'],
      'cat_name'  => $categories->fields['categories_name'],
      'cat_img'   => $categories->fields['categories_image'],
      'cat_href'  => $categories->fields['url_text'],
      'children'  => array(
        'count'       => 0,
        'items'       => array()
      )
    );
    
    if ($categories_array[$categories->fields['categories_id']]['cat_img'] != '')
      $categories_array[$categories->fields['categories_id']]['cat_img'] = href_link(DIR_WS_IMAGES.'categories/'.($img_subpath!='' ? $img_subpath.'/' : '').$categories_array[$categories->fields['categories_id']]['cat_img']);

    $sub_items = get_categories($categories->fields['categories_id'], $level+1, $img_subpath,$flat);
    if (count($sub_items) > 0) {
    if ($flat)
      $categories_array = array_merge($categories_array, $sub_items);
    else
      $categories_array[$categories->fields['categories_id']]['children'] = array(
        'count'       => count($sub_items),
        'items'       => $sub_items
      );
    }

		$categories->MoveNext();
	}

	return $categories_array;
}

/*
function get_categories($categories_array = '', $parent_id = '0', $indent = '') {
	global $db;

	$parent_id = $parent_id;

	if (!is_array($categories_array))
		$categories_array = array();

	$categories = $db->db_query("SELECT
									c.categories_id,
									cd.categories_name
								FROM
									".TABLE_CATEGORIES." c,
									".TABLE_CATEGORIES_DESCRIPTION." cd
								WHERE
									parent_id = ".$db->db_prepare($parent_id)."
								AND
									c.categories_id = cd.categories_id
								AND
									c.categories_status != 0
								AND
									cd.language_id = '".$_SESSION['languages_id']."'
								ORDER BY
									sort_order, cd.categories_name");

	while(!$categories->EOF) {
		$categories_array[] = array('id' => $categories->fields['categories_id'], 'text' => $indent.$categories->fields['categories_name']);

		if ($categories->fields['categories_id'] != $parent_id)
			$categories_array = get_categories($categories_array, $categories->fields['categories_id'], $indent.'&nbsp;&nbsp;');
		$categories->MoveNext();
	}

	return $categories_array;
}
*/
?>